<?php
	include 'db.php';
	setcookie("username_thien",$name,time()-3600*24*30,'/');
	if(isset($_POST['ok'])){
		$name = $email = $pass= "";
		$mgs="";
		//lay gia tri nhap vao
		$name=mysql_real_escape_string($_POST['username']);
		$email=mysql_real_escape_string($_POST['email']);
		$pass=md5(mysql_real_escape_string($_POST['password']));
		if($name && $email && $pass){
			$sql="SELECT * FROM dhv_nguyenvanthien_user WHERE username='".$name."'";
			$query=mysql_query($sql);
			if(mysql_num_rows($query) == 1){
				$mgs="<p>tên đăng nhập đã tồn tại</p>";
			}
			else{
				$sql2="INSERT INTO dhv_nguyenvanthien_user(username,password,email) VALUES('".$name."','".$pass."','".$email."')";
				$query2=mysql_query($sql2);
				if($query2){
					$href="http://" .$_SERVER['HTTP_HOST']. "/group4/nguyenvanthien/issue7.php";
					$mgs="<p>bạn đã đăng ký thành công <a href='".$href."'>Click to login</a></p>";
					//setcookie("username_thien",$name,time()-3600*24*30,'/');
					//setcookie("email_thien",$email,time()-3600*24*30,'/');
				}
				
			}  
		}
	}//end if

	
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
	<meta charset="UTF-8">
	<title></title>
	<link type="text/css" href="css/style.css" rel="stylesheet" />
	<script type="text/javascript" src="js/jquery-1.10.2.js" ></script>
	<script type="text/javascript" src="js/jquery.validate.min.js" ></script>
	<script type="text/javascript">
		$(document).ready(function(){
			$("#input_form").validate(); 
		})
	</script> 
</head>
<body>
	<div class="register">
		<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" id='input_form' method='post'>
			<fieldset>
				<legend>Form Register</legend>
				<label for="username">Username:</label><br/>
				<input type="text" value="" name="username" id="username" size="25" class="required" /><br/>
				<label for="password">Password:</label><br/>
				<input type="password" value="" name="password" id="password" size="25" class="required" minlength='6'  /><br/>
				<label for="cpassword">Re Password :</label><br/>
				<input type="password" value="" name="cpassword" id="cpassword" size="25" equalTo="#password"/><br/>
				<label for="email">Email :</label><br/>
				<input type="email" value="" name="email" id="email" size="25" class="required"  /><br/>
				<input type="submit" name="ok" value="Register"/>
				<?php 
					if(isset($mgs)) echo $mgs;
				?>
			</fieldset>
		</form> 
	</div>
</body>
</html>